package com.sfeir.gwt.webding.client.showcase;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.gwt.webding.client.component.formator.Formator;
import com.sfeir.gwt.webding.client.component.formator.FormatorHandler;
import com.sfeir.gwt.webding.client.component.formator.LowerCaseFormator;
import com.sfeir.gwt.webding.client.component.formator.RegexFormator;
import com.sfeir.gwt.webding.client.component.formator.TrimFormator;
import com.sfeir.gwt.webding.client.component.formator.UpperCaseFormator;
import com.sfeir.gwt.webding.client.component.label.LabelTextBox;
import com.sfeir.gwt.webding.client.showcase.ShowcaseAnnotations.ShowcaseSource;


public class TabFormator extends LazyTab {
    /**
     * 
     */
    @ShowcaseSource
    protected Widget createTab() {
        VerticalPanel panel = new VerticalPanel();

        LabelTextBox field1 = new LabelTextBox("Majuscule formateur");
        field1.addFormator(new UpperCaseFormator());
        panel.add(field1);

        LabelTextBox field2 = new LabelTextBox("Majuscule formateur avec valeur de départ");
        field2.addFormator(new UpperCaseFormator());
        field2.setValue("toto");
        panel.add(field2);

        LabelTextBox field3 = new LabelTextBox("Minuscule formateur");
        field3.addFormator(new LowerCaseFormator());
        field3.setValue("ToTo");
        panel.add(field3);

        LabelTextBox field4 = new LabelTextBox("Trim formateur");
        field4.addFormator(new TrimFormator());
        field4.setValue("   ToTo    ");
        panel.add(field4);

        LabelTextBox field5 = new LabelTextBox("Regex formateur");
        field5.addFormator(new RegexFormator("([^a-zA-Z0-9]|[-])+", "-"));
        field5.setValue("T_o!T'o ");
        panel.add(field5);

        FormatorHandler<String> customFormator = new Formator<String>() {
            public String format(String value) {
                int i, len = value.length();
                StringBuffer dest = new StringBuffer(len);

                for (i = (len - 1); i >= 0; i--)
                    dest.append(value.charAt(i));
                return dest.toString();
            }
        };

        LabelTextBox field6 = new LabelTextBox("Formateur Personalisé (Retourne le texte)");
        field6.addFormator(customFormator);
        field6.setValue("fedcba");
        panel.add(field6);

        LabelTextBox field7 = new LabelTextBox("Plusieurs Formateurs");
        field7.addFormator(customFormator);
        field7.addFormator(new TrimFormator());
        field7.addFormator(new UpperCaseFormator());
        field7.addFormator(new RegexFormator("([^a-zA-Z0-9]|[-])+", "-"));
        field7.setValue("  fed'!:cba  ");
        panel.add(field7);
        

        LabelTextBox field8 = new LabelTextBox("Formateur Personalisé");
        field8.addFormator(new Formator<String>() {
            @Override
            public String format(String value) {
                return value.replace("a", "b");
            }
        });
        panel.add(field8);

        return panel;
    }

    @Override
    public String getDescription() {
        return "Formateurs du champs après la saisie";
    }

    @Override
    public String getName() {
        return "Formateurs";
    }
}